package com.ruoyi.qiuyue.controller.qiuyue;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.qiuyue.common.MusicConstat;
import com.ruoyi.qiuyue.domain.MusicSong;
import com.ruoyi.qiuyue.service.IMusicSongService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 音乐控制类
 */
@RestController
@RequestMapping("/app/music")
public class QMusicController extends BaseController {
    @Autowired
    public ISysConfigService configService;
    @Autowired
    public IMusicSongService musicSongService;
    @Autowired
    public RuoYiConfig ruoYiConfig;

    /**
     * 拉取歌曲分类
     * @return
     */
    @GetMapping("/type/list")
    public AjaxResult typeList(){
        String types = configService.selectConfigByKey(MusicConstat.SONG_TYPE_KEY);
        // 首页拉取歌曲分类列表
        List<String> list = Arrays.asList(types.split(MusicConstat.SONG_TYPE_SPLIT));
        Map<String,List> data = new HashMap<>();
        data.put("types",list);
        return AjaxResult.success().put("data",data);
    }

    /**
     * 根据类型分页搜索数据
     * 缓存redis
     * @param type
     * @return
     */
    @Cacheable(value = "Home",key = "#type")
    @GetMapping("/home")
    public AjaxResult home(@RequestParam("type") String type,@RequestParam("num") int num){
        List<MusicSong> musicList = musicSongService.getMusicListByType(type, num);
        Map<String,List> data = new HashMap<>();
        data.put("musics",musicList);
        return AjaxResult.success().put("data",data);
    }

    /**
     * 根据分类搜索歌曲 分页
     * @param type
     * @return
     */
    @GetMapping("/search")
    public AjaxResult search(@RequestParam(value = "type") String type){
        startPage();
        List<MusicSong> musicList = musicSongService.getMusicListByType(type);
        Map<String,List> data = new HashMap<>();
        data.put("musics",musicList);
        return AjaxResult.success().put("data",data);
    }

    /**
     * 根据歌手名或歌名模糊搜索
     * @param query
     * @return
     */
    @GetMapping("/searches")
    public AjaxResult searches(@RequestParam(value = "query") String query){
        startPage();
        List<MusicSong> searchData = musicSongService.searchByNameOrAuthor(query);
        Map<String,List> data = new HashMap<>();
        data.put("search",searchData);
        return AjaxResult.success().put("data",data);
    }
}
